﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule.CPR
{
    public class CPR_Form_Manager : BusinessLogic
    {

        /// <summary>
        /// 获取CPR_FROM_DATAS
        /// </summary>
        /// <returns></returns>
        public DataTable GetCPR_FROM_Table(string visitid,string domainID,string active ,string strWhere)
        {
            string sql = @"select e.NAME as BRAND_NAME,d.NAME,c.* from (
                        select b.MEASUREMENTNAME,b.SEQUENCE,b.ACTIVE,b.INPUTTYPE,
                        a.ID,FORM_CONFIG_ID,a.CUSTOMERS_ID,a.FORM_ID,a.VISITID,a.VALUE,a.VALUE_TYPE,a.PHOTO_ID,a.DOMAIN_ID, 
                        a.LAST_UPDATER,a.LAST_UPDATE_TIME,a.CREATE_TIME
                        from  CPR_FORM_DATAS a, CPR_FORM_CONFIG  b 
                        where a.FORM_CONFIG_ID = b.ID  and b.domain_id={1}
                        ) c inner join FORMS d
                        inner join BRANDS e
                        on d.BRAND_CODE=e.CODE and d.DOMAIN_ID={1}
                        on c.FORM_ID = d.ID  and c.VISITID='{0}' and c.DOMAIN_ID={1} and e.DOMAIN_ID={1}  and c.ACTIVE={2} {3} order by BRAND_NAME,NAME,c.SEQUENCE
                        ";
            sql = string.Format(sql,visitid, domainID,active,strWhere);

            return SqlData.OpenSql(sql).Tables[0];
        }


        public CPR_FORM_CONFIG Load(int id)
        {
            return (CPR_FORM_CONFIG)ObjectRow.Load(typeof(CPR_FORM_CONFIG), string.Format("this.ID='{0}'", id));
        }

        public CPR_FORM_CONFIG Load(decimal domain_id, string code)
        {
            return (CPR_FORM_CONFIG)ObjectRow.Load(typeof(CPR_FORM_CONFIG), string.Format("this.DomainID='{0}' and this.CODE='{1}'", domain_id, code));
        }

        public bool Save(CPR_FORM_CONFIG entity)
        {
            try
            {

                if (entity.State == DataRowState.Added)
                {
                    entity.CREATE_TIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
                }
                entity.Save();

                return true;
            }
            catch (Exception ex)
            {
                entity.Connection.RollbackTransaction();
                this.message = ex.Message;
                throw ex;
            }
        }

        public void CreateTable(string tableName)
        {
            if (!string.IsNullOrEmpty(tableName))
            {
                SqlData.ExecuteSP("SP_CREATE_CPR", tableName);
            }
        }

        public ObjectTable GetCPRConfig(string domainID)
        {

            return new ObjectTable(typeof(CPR_FORM_CONFIG), string.Format("DOMAIN_ID={0}  order by SEQUENCE ", domainID));
        }


        public ObjectTable GetCPRConfig(string domainID, string active)
        {

            return new ObjectTable(typeof(CPR_FORM_CONFIG), string.Format("DOMAIN_ID={0} AND ACTIVE='{1}' order by SEQUENCE ", domainID, active));
        }

        /// <summary>
        /// 从最后一张表中删除对应数据
        /// </summary>
        public void DeleteLastData(string customerID, string tablename, decimal domainID)
        {
            try
            {
                string sql = @" delete from {0}  where  CUSTOMERS_ID={1}  AND DOMAIN_ID={2}";

                sql = string.Format(sql, tablename, customerID, domainID);
                SqlData.OpenSql(sql);
            }
            catch (Exception ex) { throw ex; }

        }

        /// <summary>
        /// 从最后一张表中插入对应数据
        /// </summary>
        public void InsertCPRData(string customerID, string procuctID, string value, string vid, string tablename, decimal domainID, string userid)
        {
            string sql = "";
            try
            {
                sql = @" insert into  {4} (CUSTOMERS_ID, PRODUCTS_ID, VALUE, VISITID, ACTIVE, DOMAIN_ID, LAST_UPDATER, LAST_UPDATE_TIME, CREATE_TIME) values({5},{6},{7},{8},'{9}',{10},'{11}',{12},{13}) ";
                decimal time = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                sql = string.Format(sql, tablename, customerID, procuctID, domainID, tablename, customerID, procuctID, value, vid, "1", domainID, userid, time, time);
                SqlData.OpenSql(sql);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message + tablename + " " + sql);
            }

        }

        /// <summary>
        /// 从最后一张表中插入对应数据
        /// </summary>
        public void InsertLastCPRData(string customerID, string procuctID, string value, string vid, string tablename, decimal domainID, string userid)
        {
            string sql = "";
            try
            {
                sql = @" delete from {0} where CUSTOMERS_ID={1} AND PRODUCTS_ID={2} AND DOMAIN_ID={3}; insert into  {4} (CUSTOMERS_ID, PRODUCTS_ID, VALUE, VISITID, ACTIVE, DOMAIN_ID, LAST_UPDATER, LAST_UPDATE_TIME, CREATE_TIME) values({5},{6},{7},{8},'{9}',{10},'{11}',{12},{13}) ";
                decimal time = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                sql = string.Format(sql, tablename, customerID, procuctID, domainID, tablename, customerID, procuctID, value, vid, "1", domainID, userid, time, time);
                SqlData.OpenSql(sql);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message + tablename + " " + sql);
            }

        }


    }
}
